import json

from bs4 import BeautifulSoup
from loguru import logger

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.processor.base_processor import BaseProcessor
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.util.muilty_coroutine_util import concurrency
from com.arcfox.util.util import random_sleep


class EnrollMentPlainProcessor(BaseProcessor):

    def __init__(self):
        super().__init__()

    @concurrency(5)
    async def store_enroll_plan(self, **result):
        task = result['task']
        mapping = db.MysqlMapping("tb_enrollment_plan")
        university_uid = self.generate_school_uid(task["university_name"])
        exist = await mapping.query(
            {
                "university_uid": university_uid,
                "province_name": task["province_name"],
                "plan_year": task["plan_year"],
                "plan_major_code": task["plan_major_code"]
            }, ["id"])
        if exist:
            logger.info(" 已经存在{}", exist)
            return
        task["university_uid"] = university_uid
        for i in range(10):
            try:
                await mapping.insert(task)
                break
            except Exception as e:
                logger.error("数据插入失败:{}", e)
                await random_sleep(1)

